package com.feng.demo.jakartacommons.lang;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import java.io.*;

public class HttpClientDemo {

	private static String url = "https://www.paypal.com";

	public static void main(String[] args) throws IOException {

		// create an instance of HttpClient.
		HttpClient client = new HttpClient();

		// create a method instance.
		HttpMethod method = new GetMethod(url);

		// execute the method.
		int statusCode = -1;
		int attempt = 0;

		// retry up to 3 times.
		while (statusCode == -1 && attempt < 3) {
			try {

				// execute the method.
				statusCode = client.executeMethod(method);
			} catch (HttpRecoverableException e) {

				System.err.println(
					"A recoverable exception occurred, retrying." +
					e.getMessage());
			} catch (IOException e) {

				System.err.println("Failed to download file.");
				e.printStackTrace();
				System.exit(-1);
			}
		}

		// check that we didn't run out of retries.
		if (statusCode == -1) {
			System.err.println("Failed to recover from exception.");
			System.exit(-2);
		}

		// read the response body.
		byte[] responseBody = method.getResponseBody();

		// release the connection.
		method.releaseConnection();

		// deal with the response.
		System.err.println(new String(responseBody));
	}
}
